layui.define(["form", "layer", "admin", "table", "upload", "element", "laydate"], function (exports) {
    var $ = layui.$,
        form = layui.form,
        layer = layui.layer,
        admin = layui.admin,
        table = layui.table,
        laydate = layui.laydate;

    var tableOption = {
        cols: [[
            {type: "checkbox"},
            {field: 'id', title: 'ID', width: 80, hide: true},
            {field: 'waybillNo', title: '运单号', width: 160},
            {field: 'channelCode', title: '渠道', hide: true},
            {field: 'channelName', title: '渠道', width: 120},
            {field: 'storeCode', title: '仓库', hide: true},
            {field: 'storeName', title: '仓库', width: 140},
            {field: 'receiverId', title: '收件人', hide: true},
            {field: 'receiverName', title: '收件人', width: 140},
            {field: 'expressNo', title: '快递号', width: 140},
            {field: 'state', title: '状态', templet: '#bill-express-list-table-state-tmp'},
            {field: 'createTime', title: '订单时间'},
            {title: '操作', width: 200, templet: '#bill-express-list-table-operate-tmp'}
        ]],
        page: true,
        limit: 50
    }

    laydate.render({
        elem: '#bill-express-list-query-form input[name=startDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-1 input[name=startDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-2 input[name=startDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-3 input[name=startDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-4 input[name=startDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-5 input[name=startDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-7 input[name=startDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form input[name=endDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-1 input[name=endDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-2 input[name=endDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-3 input[name=endDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-4 input[name=endDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-5 input[name=endDate]' //指定元素
    });

    laydate.render({
        elem: '#bill-express-list-query-form-7 input[name=endDate]' //指定元素
    });

    admin.req({
        url: "/bill/countBilExpress",
        data: {},
        done: function (res) {
            $("#bill-express-list-table-all-count").html(res.data);
        }
    });

    admin.req({
        url: "/bill/countBilExpress",
        data: {"states": "1"},
        done: function (res) {
            $("#bill-express-list-table-1-count").html(res.data);
        }
    });

    admin.req({
        url: "/bill/countBilExpress",
        data: {"states": "2"},
        done: function (res) {
            $("#bill-express-list-table-2-count").html(res.data);
        }
    });

    admin.req({
        url: "/bill/countBilExpress",
        data: {"states": "3"},
        done: function (res) {
            $("#bill-express-list-table-3-count").html(res.data);
        }
    });

    admin.req({
        url: "/bill/countBilExpress",
        data: {"states": "4"},
        done: function (res) {
            $("#bill-express-list-table-4-count").html(res.data);
        }
    });

    admin.req({
        url: "/bill/countBilExpress",
        data: {"states": "5,6"},
        done: function (res) {
            $("#bill-express-list-table-5-count").html(res.data);
        }
    });

    admin.req({
        url: "/bill/countBilExpress",
        data: {"states": "7"},
        done: function (res) {
            $("#bill-express-list-table-7-count").html(res.data);
        }
    });

    table.render($.extend({}, tableOption, {
        id: "bill-express-list-table-all",
        elem: '#bill-express-list-table-all',
        url: "/bill/listBilExpress",
        where: {
            access_token: layui.data('layuiAdmin').access_token
        }
    }));

    table.render($.extend({}, tableOption, {
        id: "bill-express-list-table-1",
        elem: '#bill-express-list-table-1',
        url: "/bill/listBilExpress",
        where: {
            states: "1",
            access_token: layui.data('layuiAdmin').access_token
        }
    }));

    table.render($.extend({}, tableOption, {
        id: "bill-express-list-table-2",
        elem: '#bill-express-list-table-2',
        url: "/bill/listBilExpress",
        where: {
            states: "2",
            access_token: layui.data('layuiAdmin').access_token
        }
    }));

    table.render($.extend({}, tableOption, {
        id: "bill-express-list-table-3",
        elem: '#bill-express-list-table-3',
        url: "/bill/listBilExpress",
        where: {
            states: "3",
            access_token: layui.data('layuiAdmin').access_token
        }
    }));

    table.render($.extend({}, tableOption, {
        id: "bill-express-list-table-4",
        elem: '#bill-express-list-table-4',
        url: "/bill/listBilExpress",
        where: {
            states: "4",
            access_token: layui.data('layuiAdmin').access_token
        }
    }));

    table.render($.extend({}, tableOption, {
        id: "bill-express-list-table-5",
        elem: '#bill-express-list-table-5',
        url: "/bill/listBilExpress",
        where: {
            states: "5,6",
            access_token: layui.data('layuiAdmin').access_token
        }
    }));

    table.render($.extend({}, tableOption, {
        id: "bill-express-list-table-7",
        elem: '#bill-express-list-table-7',
        url: "/bill/listBilExpress",
        where: {
            states: "7",
            access_token: layui.data('layuiAdmin').access_token
        }
    }));

    table.on("tool(bill-express-list-table-all)", function (obj) {
        if (obj.event === "follow") {
            openLogisticsList(obj);
        } else if (obj.event === "delete") {
            deleteBilExpress(obj);
        }
    });

    table.on("tool(bill-express-list-table-1)", function (obj) {
        if (obj.event === "follow") {
            openLogisticsList(obj);
        } else if (obj.event === "delete") {
            deleteBilExpress(obj);
        }
    });

    table.on("tool(bill-express-list-table-2)", function (obj) {
        if (obj.event === "follow") {
            openLogisticsList(obj);
        } else if (obj.event === "delete") {
            deleteBilExpress(obj);
        }
    });

    table.on("tool(bill-express-list-table-3)", function (obj) {
        if (obj.event === "follow") {
            openLogisticsList(obj);
        } else if (obj.event === "delete") {
            deleteBilExpress(obj);
        }
    });

    table.on("tool(bill-express-list-table-4)", function (obj) {
        if (obj.event === "follow") {
            openLogisticsList(obj);
        } else if (obj.event === "delete") {
            deleteBilExpress(obj);
        }
    });

    table.on("tool(bill-express-list-table-5)", function (obj) {
        if (obj.event === "follow") {
            openLogisticsList(obj);
        } else if (obj.event === "delete") {
            deleteBilExpress(obj);
        }
    });

    table.on("tool(bill-express-list-table-7)", function (obj) {
        if (obj.event === "follow") {
            openLogisticsList(obj);
        } else if (obj.event === "delete") {
            deleteBilExpress(obj);
        }
    });

    form.on("submit(bill-express-list-query-submit)", function (obj) {
        layer.load(2, {time: 100 * 1000});

        var data = obj.field;
        data["access_token"] = layui.data('layuiAdmin').access_token;

        table.reload("bill-express-list-table-all", {
            where: data,
            page: {
                curr: 1
            },
            done: function () {
                layer.closeAll('loading');
            }
        });

        return false;
    });

    form.on("submit(bill-express-list-query-submit-1)", function (obj) {
        layer.load(2, {time: 100 * 1000});

        var data = obj.field;
        data["access_token"] = layui.data('layuiAdmin').access_token;
        data["states"] = "1";

        table.reload("bill-express-list-table-1", {
            where: data,
            page: {
                curr: 1
            },
            done: function () {
                layer.closeAll('loading');
            }
        });

        return false;
    });

    form.on("submit(bill-express-list-query-submit-2)", function (obj) {
        layer.load(2, {time: 100 * 1000});

        var data = obj.field;
        data["access_token"] = layui.data('layuiAdmin').access_token;
        data["states"] = "2";

        table.reload("bill-express-list-table-2", {
            where: data,
            page: {
                curr: 1
            },
            done: function () {
                layer.closeAll('loading');
            }
        });

        return false;
    });

    form.on("submit(bill-express-list-query-submit-3)", function (obj) {
        layer.load(2, {time: 100 * 1000});

        var data = obj.field;
        data["access_token"] = layui.data('layuiAdmin').access_token;
        data["states"] = "3";

        table.reload("bill-express-list-table-3", {
            where: data,
            page: {
                curr: 1
            },
            done: function () {
                layer.closeAll('loading');
            }
        });

        return false;
    });

    form.on("submit(bill-express-list-query-submit-4)", function (obj) {
        layer.load(2, {time: 100 * 1000});

        var data = obj.field;
        data["access_token"] = layui.data('layuiAdmin').access_token;
        data["states"] = "4";

        table.reload("bill-express-list-table-4", {
            where: data,
            page: {
                curr: 1
            },
            done: function () {
                layer.closeAll('loading');
            }
        });

        return false;
    });

    form.on("submit(bill-express-list-query-submit-5)", function (obj) {
        layer.load(2, {time: 100 * 1000});

        var data = obj.field;
        data["access_token"] = layui.data('layuiAdmin').access_token;
        data["states"] = "5,6";

        table.reload("bill-express-list-table-5", {
            where: data,
            page: {
                curr: 1
            },
            done: function () {
                layer.closeAll('loading');
            }
        });

        return false;
    });

    form.on("submit(bill-express-list-query-submit-7)", function (obj) {
        layer.load(2, {time: 100 * 1000});

        var data = obj.field;
        data["access_token"] = layui.data('layuiAdmin').access_token;
        data["states"] = "7";

        table.reload("bill-express-list-table-7", {
            where: data,
            page: {
                curr: 1
            },
            done: function () {
                layer.closeAll('loading');
            }
        });

        return false;
    });

    function openLogisticsList(obj) {
        layer.open({
            type: 2,
            title: "运单物流信息",
            area: ['800px', '500px'],
            content: layui.setter.base + 'views/bil/logisticsList.html?waybillNo=' + obj.data.waybillNo,
            cancel: function(index, layero){
                layer.close(index);
                return false;
            }
        });
    }

    function deleteBilExpress(obj) {
        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
            layer.close(index);

            var data = {"waybillNo": obj.data.waybillNo};
            admin.req({
                url: "/bill/deleteBilExpress",
                data: data,
                beforeSend: function () {
                    layer.load(2, {time: 100 * 1000});
                },
                complete: function () {
                    layer.closeAll('loading');
                },
                done: function () {
                    $("#bill-express-list-query-btn").trigger("click");
                    $("#bill-express-list-query-btn-1").trigger("click");
                    $("#bill-express-list-query-btn-2").trigger("click");
                    $("#bill-express-list-query-btn-3").trigger("click");
                    $("#bill-express-list-query-btn-4").trigger("click");
                    $("#bill-express-list-query-btn-5").trigger("click");
                    $("#bill-express-list-query-btn-7").trigger("click");
                }
            });
        });
    }

    table.render({
        elem: '#bill-express-list-table-create',
        url: "/bill/listBilExpress",
        cols: [[
            {field:'id', title: 'ID', width:80, hide: true},
            {field:'waybillNo', title: '运单号'},
            {field:'channelCode', title: '渠道', hide: true},
            {field:'channelName', title: '渠道'},
            {field:'storeCode', title: '仓库', hide: true},
            {field:'storeName', title: '仓库'},
            {field:'receiverId', title: '收件人', hide: true},
            {field:'receiverName', title: '收件人'},
            {field:'state', title: '状态', hide: true},
            {field:'stateDescription', title: '状态'},
            {title: '操作', width: 200, templet: '#bill-express-list-table-operate-tmp'}
        ]],
        id: 'bill-express-list-table-create',
        page: true,
        limit: 50,
        where: {
            access_token: layui.data('layuiAdmin').access_token,
            status: "create"
        }
    });

    exports("expressLis", {});
});